﻿using Newtonsoft.Json;
using Simple.Tools.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Simple.Tools

/// <summary>
/// 版 本 钢易智企2.0 V 2.0.0 钢易智企2.0
/// Copyright (c) 2023 杭州钢易科技有限公司
/// 创建人：曾东明
/// 日 期：2024/3/14 13:28:20
/// 描 述： 敞亮定义
/// </summary>

{
    /// <summary>
    /// 
    /// </summary>
    public static class SimpleConstant
    {
        /// <summary>
        /// 基础类型
        /// </summary>
        private static Dictionary<string, Type> _systemType;

        private static Dictionary<int,Dictionary<int,KeyValue<int,string>>> _mapTypeConstant;

        private static int _int32DefaultNull;
        private static byte _byteDefaultNull;
        private static short _shortDefaultNull;
        private static float _floatDefaultNull;
        private static double _doubleDefaultNull;
        private static long _longDefaultNull;
        private static Guid _guidDefaultNull;
        private static decimal _decimalDefaultNull;
        private static DateTime _dateTimeDefaultNull;
        private static TimeSpan _timeSpanDefaultNull;
        private static string _stringDefaultNull;

        private static List<char> _numberAndLetter;
        /// <summary>
        /// 
        /// </summary>
        static SimpleConstant()
        {
            InitDefaultValue();
            InitBaseType();
            _mapTypeConstant = new Dictionary<int, Dictionary<int, KeyValue<int, string>>>();

			_numberAndLetter = new List<char>();

			for (int i = 48; i <= 57; i++)
			{
				_numberAndLetter.Add((char)i);
			}


            //A-Z
			for (int i = 65; i <= 90; i++)
            {
				_numberAndLetter.Add((char)i);
			}
            // a-z
			for (int i = 97; i <= 122; i++)
			{
				_numberAndLetter.Add((char)i);
			}

		}

        #region 基础类型默认值设置

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public static void SetDefault(this int value)
        {
            _int32DefaultNull = value;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public static void SetDefault(this long value)
        {
            _longDefaultNull = value;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public static void SetDefault(this short value)
        {
            _shortDefaultNull = value;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public static void SetDefault(this byte value)
        {
            _byteDefaultNull = value;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>

        public static void SetDefault(this float value)
        {
            _floatDefaultNull = value;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        
        public static void SetDefault(this double value)
        {
            _doubleDefaultNull = value;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        
        public static void SetDefault(this decimal value)
        {
            _decimalDefaultNull = value;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public static void SetDefault(this DateTime value)
        {
            _dateTimeDefaultNull = value;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>

        public static void SetDefault(this TimeSpan value)
        {
            _timeSpanDefaultNull = value;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public static void SetDefault(this Guid value)
        {
            _guidDefaultNull = value;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public static void SetDefault(this string value)
        {
            _stringDefaultNull = value;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static long GetDefaultLong()
        {
            return _longDefaultNull;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static int GetDefaultInt()
        {
            return _int32DefaultNull;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static short GetDefaultShort()
        {
            return _shortDefaultNull;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static byte GetDefaultByte()
        {
            return _byteDefaultNull;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static float GetDefaultFloat()
        {
            return _floatDefaultNull;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static double GetDefaultDouble()
        {
            return _doubleDefaultNull;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static decimal GetDefaultDecimal()
        {
            return _decimalDefaultNull;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static DateTime GetDefaultDateTime()
        {
            return _dateTimeDefaultNull;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        
        public static TimeSpan GetDefaultTimeSpan()
        {
            return _timeSpanDefaultNull;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        
        public static Guid GetDefaultGuid()
        {
            return _guidDefaultNull;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static string GetDefaultString()
        {
            return _stringDefaultNull;
        }

        #endregion

        /// <summary>
        /// 系统基础类型
        /// </summary>
        /// <param name="type"></param>
        public static void SetSystemType(Type type)
        {
            if (type==null)
            {
                return;
            }
            if (!_systemType.ContainsKey(type.FullName))
            {
                _systemType.Add(type.FullName, type);
            }
        }
        /// <summary>
        /// 获取系统默认类型
        /// </summary>
        public static Dictionary<string,Type> GetSystemType()
        {
            return _systemType;
        }
        /// <summary>
        /// 初始化值类型 默认为空的值
        /// </summary>
        public static void InitDefaultValue()
        {
            #region 初始化 默认值设置
            _int32DefaultNull = -1;
            _byteDefaultNull = byte.MinValue;
            _shortDefaultNull = -1;
            _floatDefaultNull = -1f;
            _dateTimeDefaultNull = DateTime.MinValue;
            _timeSpanDefaultNull = TimeSpan.MinValue;
            _longDefaultNull = -1;
            _guidDefaultNull = Guid.Empty;
            _decimalDefaultNull = -1;
            _doubleDefaultNull = -1;
            _stringDefaultNull= string.Empty;

            #endregion
        }


        /// <summary>
        /// 初始化基础类型
        /// </summary>
        public static void InitBaseType()
        {

            if (SimpleCheck.IsNull(_systemType))
            {
                _systemType = new Dictionary<string, Type>();
            }
            Type intType = typeof(int);
            Type floatType = typeof(float);
            Type doubleType = typeof(double);
            Type decimalType = typeof(decimal);
            Type dateTimeType = typeof(DateTime);
            Type timeSpanType = typeof(TimeSpan);
            Type shortType = typeof(short);
            Type longType = typeof(long);
            Type stringType = typeof(string);
            Type guidType = typeof(Guid);
            Type boolType = typeof(bool);

            _systemType.Add(intType.FullName, intType);
            _systemType.Add(floatType.FullName, floatType);
            _systemType.Add(doubleType.FullName, doubleType);
            _systemType.Add(decimalType.FullName, decimalType);
            _systemType.Add(dateTimeType.FullName, dateTimeType);
            _systemType.Add(timeSpanType.FullName, timeSpanType);
            _systemType.Add(shortType.FullName, shortType);
            _systemType.Add(longType.FullName, longType);
            _systemType.Add(stringType.FullName, stringType);
            _systemType.Add(guidType.FullName, guidType);
            _systemType.Add(boolType.FullName, boolType);
        }

		/// <summary>
		/// 填写相对路径如果找不到文件系统将会补全路径
		/// </summary>
		/// <param name="Path"></param>

		public static void InitTypeConstant(string Path)
        {
            string value=SimpleCheck.ReadAllText(Path);
            Dictionary<int, Dictionary<int, KeyValue<int, string>>> map = JsonConvert.DeserializeObject<Dictionary<int, Dictionary<int, KeyValue<int, string>>>>(value);
            InitTypeConstant(map);
        }

        /// <summary>
        /// 常量初始化
        /// </summary>
        /// <param name="map"></param>
        public static void InitTypeConstant(Dictionary<int, Dictionary<int, KeyValue<int, string>>> map)
        {
            if (SimpleCheck.IsNull(map))
            {
                return;
            }
            foreach (var item in map)
            {
                if (!_mapTypeConstant.ContainsKey(item.Key))
                {
                    _mapTypeConstant.Add(item.Key, item.Value);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="main"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static IKeyValue<int,string> GetKeyValue(int main,int key)
        {
            if (_mapTypeConstant.ContainsKey(main))
            {
                if (_mapTypeConstant[main].ContainsKey(key))
                {
                    return _mapTypeConstant[main][key];
                }
            }
            return null;
        }

        /// <summary>
        /// 返回类型对应的value值
        /// </summary>
        /// <param name="main"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetValue(int main, int key)
        {
            IKeyValue<int,string> value=GetKeyValue(main,key);

            if (!SimpleCheck.IsNull(value))
            {
                return value.Value;
            }
            return "没有该类型的值,请进行配置相应的类型的值!";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="main"></param>
        /// <returns></returns>
        public static Dictionary<int,KeyValue<int,string>> GetValues(int main)
        {
            Dictionary<int, KeyValue<int, string>> values = new Dictionary<int, KeyValue<int, string>>();
            if (_mapTypeConstant.ContainsKey(main))
            {
                values=_mapTypeConstant[main];
            }
            return values;
        }

        /// <summary>
        /// 返回list值
        /// </summary>
        /// <param name="main"></param>
        /// <returns></returns>
        public static List<IKeyValue<int, string>> ToValueList(int main)
        {
            List<IKeyValue<int, string>> values = new List<IKeyValue<int, string>>();
            if (_mapTypeConstant.ContainsKey(main))
            {
                foreach (var item in _mapTypeConstant[main])
                {
                    values.Add(item.Value);
                }
            }
            return values;
        }

        /// <summary>
        /// 数字和字符
        /// </summary>
        /// <returns></returns>
		public static List<char> GetNumberAndLetter()
		{
			return _numberAndLetter;
		}



	}
 
}
